//Author: Linuxayn

#include "stdio.h"
	
#define MaxNumArgv 5
#define MaxArgvLen 30
	
int split(char *buff,char argv[][MaxArgvLen])
{
	int t=0;
	int pos=0;
	int pos_n=0;
	while(buff[pos])
	{
		while(buff[pos] && buff[pos]==' ')
			pos++;
		while(buff[pos] && buff[pos]!=' ')
		{
			argv[t][pos_n++]=buff[pos++];
		}
		argv[t][pos_n]=0;
		pos_n=0;
		t++;
	}
	return t;
}

int main()
{
	SpaceId newProc;
	char buff[100];
	int argc,i;
	char *argv[MaxNumArgv];
	char temp[MaxNumArgv][MaxArgvLen];
	char prompt[2]="#";
	
	for(i=0;i<MaxNumArgv;i++)
		argv[i]=temp[i];
	
	while(1)
	{
		Write(prompt,1,output);
		
		Gets(buff);
		argc=split(buff,temp);
		if(argc>0)
		{
			if(strcmp(argv[0],"exit")==0)  //exit the shell
			{
				break;
			}
			else if(strcmp(argv[0],"ls")==0) //show the files' list of current location
			{
				Ls();
			}
			else if(strcmp(argv[0],"rm")==0) //delete(remove) a file
			{
				if(argc==2)
					Delete(argv[1]);
				else
					Print("Only need 1 arguement.\n");
			}
			else if(strcmp(argv[0],"cp")==0)  //copy a file from outside to the "virtual" disk
			{
				if(argc==3)
					CpyOSFile(argv[1],argv[2]);
				else
					Print("2 arguements required.\n");
			}
			else if(strcmp(argv[0],"help")==0)
			{
				Print("HELP  EXIT  LS  RM  CP");
			}
			else if(argv[0][0]=='&') //run a program background
			{
				if(IsFileExist(argv[0]+1))
					newProc=ExecV(argc,argv);
			}
			else if(IsFileExist(argv[0])) //run a program and waits for it
			{
				newProc = ExecV(argc,argv);
				Join(newProc);
			}
			else
			{
				Print("Sorry,could not match your command.\n");
			}
		}
	}
	Halt();
	return 0;
}